/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

//import com.thienloc.omap.entity.TblConfRate;
import com.thienloc.omap.entity.TblAccountCp;
import com.thienloc.omap.entity.TblConfRate;
import com.thienloc.omap.session.ScratchAccountFacade;
import com.thienloc.omap.session.ScratchConfigRateFacade;
import com.thienloc.omap.util.JsfUtil;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author duongnv
 */
@ManagedBean
@RequestScoped
public class ScratchAccManagement extends AbstractCrudBean {

    private String userId;
    private String userPass;
    private String ip;
    private String cpName;
    private String cpAddress;
    private String cpPhone;
    private boolean lock;
    private int numConcurrentTrans;
    private int rateLevel;
    private double specialBonus;
    private List<SelectItem> lConfigRate;
    private String errMessage;
    private List<TblAccountCp> lAccs = new ArrayList<TblAccountCp>();
    @EJB
    private ScratchConfigRateFacade configFacade;
    @EJB
    private ScratchAccountFacade accFacade;

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(ScratchAccManagement.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        this.lock = false;
        lConfigRate = getRateConfig();
//        fillInput();
        lAccs = getAccList();
        specialBonus = 1.0;
    }

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected void findSelected(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void createSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void editSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void removeSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareViewAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareEditAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateAccount(ActionEvent actionEvent) throws Exception {
        int lockValue = 0;
        if (lock) {
            lockValue = 1;
        }
        TblAccountCp rUpdate = accFacade.update(userId, userPass, ip, cpName, cpAddress, cpPhone, lockValue, numConcurrentTrans, rateLevel, specialBonus);

        if (rUpdate != null) {
            lAccs = getAccList();
            clearInput();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Cập nhật dữ liệu", "Thành công."));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cập nhật dữ liệu", "Thất bại."));
        }
    }

    public void delete() throws Exception {
        String uId = JsfUtil.getRequestParameter("uId");
        if (uId != null) {
            TblAccountCp acc = accFacade.findByUserId(uId);
            if (acc != null) {
                accFacade.delete(acc);
                lAccs = getAccList();
            }
        }
    }

    public void fillInput(ActionEvent event) throws Exception {
        String uId = JsfUtil.getRequestParameter("uId");
        if (uId != null) {
            TblAccountCp acc = accFacade.findByUserId(uId);
            if (acc != null) {
                setUserId(acc.getUserId());
                this.userPass = acc.getUserPass();
                setIp(acc.getIp());
                this.cpName = acc.getCpName();
                this.cpAddress = acc.getCpAddress();
                this.cpPhone = acc.getCpPhone();
                this.lock = (acc.getIsLocked() == 0) ? false : true;
                this.numConcurrentTrans = acc.getNumConcurrentTrans();
                this.specialBonus = acc.getSpecialBonus();
                this.rateLevel = acc.getRateLevel();
            }
        }
    }

    public List<SelectItem> getlConfigRate() {
        return lConfigRate;
    }

    public void setlConfigRate(List<SelectItem> lConfigRate) {
        this.lConfigRate = lConfigRate;
    }

    public String getCpAddress() {
        return cpAddress;
    }

    public void setCpAddress(String cpAddress) {
        this.cpAddress = cpAddress;
    }

    public String getCpName() {
        return cpName;
    }

    public void setCpName(String cpName) {
        this.cpName = cpName;
    }

    public String getCpPhone() {
        return cpPhone;
    }

    public void setCpPhone(String cpPhone) {
        this.cpPhone = cpPhone;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public boolean isLock() {
        return lock;
    }

    public void setLock(boolean lock) {
        this.lock = lock;
    }

    public int getNumConcurrentTrans() {
        return numConcurrentTrans;
    }

    public void setNumConcurrentTrans(int numConcurrentTrans) {
        this.numConcurrentTrans = numConcurrentTrans;
    }

    public int getRateLevel() {
        return rateLevel;
    }

    public void setRateLevel(int rateLevel) {
        this.rateLevel = rateLevel;
    }

    public double getSpecialBonus() {
        return specialBonus;
    }

    public void setSpecialBonus(double specialBonus) {
        this.specialBonus = specialBonus;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserPass() {
        return userPass;
    }

    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    public String getErrMessage() {
        return errMessage;
    }

    public void setErrMessage(String errMessage) {
        this.errMessage = errMessage;
    }

    private List<SelectItem> getRateConfig() throws Exception {
        List<TblConfRate> config = configFacade.findAll();
        List<SelectItem> result = new ArrayList<SelectItem>();
        for (TblConfRate item : config) {
            String desc = "Áp dụng cho mức từ " + item.getBottomValue() + " tới " + item.getUpValue();
            SelectItem i = new SelectItem(item.getLevelId(), "Level " + item.getLevelId(), desc);
            result.add(i);
        }
        return result;
    }

    private List<TblAccountCp> getAccList() throws Exception {
        return accFacade.findAll1();
    }

    public List<TblAccountCp> getlAccs() {
        return lAccs;
    }

    public void setlAccs(List<TblAccountCp> lAccs) {
        this.lAccs = lAccs;
    }

    private void clearInput() {
        setUserId("");
        this.userPass = "";
        setIp("");
        this.cpName = "";
        this.cpAddress = "";
        this.cpPhone = "";
        this.lock = false;
        this.numConcurrentTrans = 0;
        this.specialBonus = 1.0;
        this.rateLevel = 1;
    }
}
